#ifndef _MSP_FLASH_H_
#define _MSP_FLASH_H_

/* includes ------------------------------------------------------------------*/
#include <stdint.h>
#include "flash_divide.h"

#define USE_STM32F40x_FLASH_SET             0                                       // 使用STM32F40x的Flash扇区配置
#define USE_STM32G0x0_FLASH_SET             0                                       // 使用STM32G0x0的Flash扇区配置
#define USE_STM32F1xx_FLASH_SET             1                                       // 使用STM32F1xx的Flash扇区配置

#if USE_STM32F40x_FLASH_SET
// 在STM32F4xx中文参考手册中，下述为 Flash模块构成（STM32F40x 和 STM32F41x）
#define ADDR_FLASH_SECTOR_0                 ((uint32_t)0x08000000)                  //扇区0起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_1                 ((uint32_t)0x08004000)                  //扇区1起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_2                 ((uint32_t)0x08008000)                  //扇区2起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_3                 ((uint32_t)0x0800C000)                  //扇区3起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_4                 ((uint32_t)0x08010000)                  //扇区4起始地址, 64 Kbytes
#define ADDR_FLASH_SECTOR_5                 ((uint32_t)0x08020000)                  //扇区5起始地址, 128 Kbytes
#define ADDR_FLASH_SECTOR_6                 ((uint32_t)0x08040000)                  //扇区6起始地址, 128 Kbytes
#define ADDR_FLASH_SECTOR_7                 ((uint32_t)0x08060000)                  //扇区7起始地址, 128 Kbytes
#define ADDR_FLASH_SECTOR_7_E               ((uint32_t)0x0807FFFF)                  // STM32F407VET6 E代表512K   (0x08000000 + 512*1024 = 0x08080000)
// stm32f407xExx只有上述8个扇区
#define ADDR_FLASH_SECTOR_8                 ((uint32_t)0x08080000)                  //扇区8起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_9                 ((uint32_t)0x080A0000)                  //扇区9起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_10                ((uint32_t)0x080C0000)                  //扇区10起始地址,128 Kbytes  
#define ADDR_FLASH_SECTOR_11                ((uint32_t)0x080E0000)                  //扇区11起始地址,128 Kbytes
#define ADDR_FLASH_SECTOR_12                ((uint32_t)0x08100000)                  //扇区12结束地址
// stm32f407xGxx只有上述12个扇区
#endif

#if USE_STM32G0x0_FLASH_SET
//FLASH起始地址
// 在STM32G070CBT6_参考手册中，STM32G070的flash固定为128K，64个扇区，故每扇区固定2K
#define FLASH_SECTOR_SIZE                   (0x800)                                 // 0x800 = 2048 = 2*1024 = 2K
#define STM32_FLASH_BASE                    ((uint32_t)0x08000000)                  // STM32G070 FLASH的起始地址
#define STM32_FLASH_END_                    ((uint32_t)(STM32_FLASH_BASE+FLASH_SECTOR_SIZE))                                // STM32G070 FLASH的结束地址
#endif

#if USE_STM32F1xx_FLASH_SET
//FLASH起始地址
#define FLASH_SECTOR_SIZE                   (0x800)                                 // 0x800 = 2048 = 2*1024 = 2K
#define STM32_FLASH_BASE                    ((uint32_t)0x08000000)                  // STM32G070 FLASH的起始地址
#define STM32_FLASH_END_                    ((uint32_t)(STM32_FLASH_BASE+FLASH_SECTOR_SIZE))                                // STM32G070 FLASH的结束地址
#endif

#define FLASH_WAITETIME                     50000                                   // FLASH等待超时时间，单位：ms

uint8_t Msp_Flash_Init(void);
uint8_t Msp_Flash_Get_Sector(uint32_t addr);
uint8_t Msp_Flash_Erase_Entire_Sector(uint32_t addrx);
uint32_t Msp_Flash_Read_Word(uint32_t ReadAddr);
uint32_t Msp_Flash_Read(uint32_t ReadAddr, uint32_t *pBuffer, uint32_t number);
uint8_t  Msp_Flash_Write_Word(uint32_t WriteAddr, uint32_t *pBuffer);
uint32_t Msp_Flash_Write(uint32_t WriteAddr, uint32_t *pBuffer, uint32_t number);

#endif
